
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>11、GRIDS · turf源码算法分析</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="lijian">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="chapter12.html" />
    
    
    <link rel="prev" href="chapter10.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    INTRODUCE
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="chapter1.html">
            
                <a href="chapter1.html">
            
                    
                    1、MEASUREMENT
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="chapter2.html">
            
                <a href="chapter2.html">
            
                    
                    2、COORDINATE MUTATION
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="chapter3.html">
            
                <a href="chapter3.html">
            
                    
                    3、TRANSFORMATION
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="chapter4.html">
            
                <a href="chapter4.html">
            
                    
                    4、FEATURE CONVERSION
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="chapter5.html">
            
                <a href="chapter5.html">
            
                    
                    5、MISC
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="chapter6.html">
            
                <a href="chapter6.html">
            
                    
                    6、HELPER
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="chapter7.html">
            
                <a href="chapter7.html">
            
                    
                    7、RANDOM
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="chapter8.html">
            
                <a href="chapter8.html">
            
                    
                    8、DATA
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="chapter9.html">
            
                <a href="chapter9.html">
            
                    
                    9、INTERPOLATION
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="chapter10.html">
            
                <a href="chapter10.html">
            
                    
                    10、JOINS
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.12" data-path="chapter11.html">
            
                <a href="chapter11.html">
            
                    
                    11、GRIDS
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="chapter12.html">
            
                <a href="chapter12.html">
            
                    
                    12、CLASSIFICATION
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="chapter13.html">
            
                <a href="chapter13.html">
            
                    
                    13、AGGREGATION
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="chapter14.html">
            
                <a href="chapter14.html">
            
                    
                    14、META
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="chapter15.md">
            
                <span>
            
                    
                    15、ASSERTIONS
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="chapter16.html">
            
                <a href="chapter16.html">
            
                    
                    16、BOOLEANS
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="chapter17.md">
            
                <span>
            
                    
                    17、UNIT CONVERSION
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >11、GRIDS</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="squaregrid">squareGrid</h1>
<p>&#x63CF;&#x8FF0;&#xFF1A;</p>
<p>&#x200B;    &#x521B;&#x5EFA;&#x6B63;&#x65B9;&#x5F62;&#x683C;&#x7F51;</p>
<p>&#x601D;&#x8DEF;&#xFF1A;</p>
<ol>
<li>&#x6839;&#x636E;bbox&#x7684;&#x8303;&#x56F4;&#x548C;&#x683C;&#x7F51;&#x7684;&#x5C3A;&#x5BF8;&#x8BA1;&#x7B97;&#x5355;&#x5143;&#x683C;&#x7684;&#x5C3A;&#x5BF8;&#xFF0C;&#x7531;&#x4E8E;bbox&#x7ED9;&#x51FA;&#x7684;&#x7ECF;&#x7EAC;&#x5EA6;&#xFF0C;&#x6240;&#x4EE5;&#x8FD9;&#x91CC;&#x8BA1;&#x7B97;&#x7684;&#x5355;&#x5143;&#x683C;&#x5C3A;&#x5BF8;&#x4E5F;&#x5E94;&#x8BE5;&#x7528;&#x5EA6;&#x8868;&#x793A;</li>
<li>&#x904D;&#x5386;&#x884C;&#x6570;&#x548C;&#x5217;&#x6570;&#xFF0C;&#x7EC4;&#x88C5;polygon</li>
</ol>
<p>&#x5B9E;&#x73B0;&#xFF1A;</p>
<pre><code class="lang-javascript">    <span class="hljs-keyword">const</span> results = [];
    <span class="hljs-keyword">const</span> west = bbox[<span class="hljs-number">0</span>];
    <span class="hljs-keyword">const</span> south = bbox[<span class="hljs-number">1</span>];
    <span class="hljs-keyword">const</span> east = bbox[<span class="hljs-number">2</span>];
    <span class="hljs-keyword">const</span> north = bbox[<span class="hljs-number">3</span>];

    <span class="hljs-comment">// &#x8BA1;&#x7B97;&#x5355;&#x5143;&#x683C;&#x5C3A;&#x5BF8;&#xFF0C;&#x4EE5;&#x5EA6;&#x8868;&#x793A;</span>
    <span class="hljs-keyword">const</span> xFraction = cellWidth / (distance([west, south], [east, south], options));
    <span class="hljs-keyword">const</span> cellWidthDeg = xFraction * (east - west);
    <span class="hljs-keyword">const</span> yFraction = cellHeight / (distance([west, south], [west, north], options));
    <span class="hljs-keyword">const</span> cellHeightDeg = yFraction * (north - south);

    <span class="hljs-comment">// &#x6839;&#x636E;bbox&#x7684;&#x957F;&#x5BBD;&#x548C;&#x5355;&#x5143;&#x683C;&#x7684;&#x5C3A;&#x5BF8;&#x8BA1;&#x7B97;&#x884C;&#x6570;&#x548C;&#x5217;&#x6570;</span>
    <span class="hljs-keyword">const</span> bboxWidth = (east - west);
    <span class="hljs-keyword">const</span> bboxHeight = (north - south);
    <span class="hljs-keyword">const</span> columns = <span class="hljs-built_in">Math</span>.floor(bboxWidth / cellWidthDeg);
    <span class="hljs-keyword">const</span> rows = <span class="hljs-built_in">Math</span>.floor(bboxHeight / cellHeightDeg);

    <span class="hljs-comment">// &#x5FAE;&#x8C03;&#x8BA1;&#x7B97;&#x884C;&#x6570;&#x548C;&#x5217;&#x6570;&#x51FA;&#x73B0;&#x7684;&#x7CBE;&#x5EA6;&#x4E22;&#x5931;</span>
    <span class="hljs-keyword">const</span> deltaX = (bboxWidth - columns * cellWidthDeg) / <span class="hljs-number">2</span>;
    <span class="hljs-keyword">const</span> deltaY = (bboxHeight - rows * cellHeightDeg) / <span class="hljs-number">2</span>;

    <span class="hljs-comment">// &#x904D;&#x5386;&#x884C;&#x6570;&#x548C;&#x5217;&#x6570;&#xFF0C;&#x7EC4;&#x88C5;polygon</span>
    <span class="hljs-keyword">let</span> currentX = west + deltaX;
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> column = <span class="hljs-number">0</span>; column &lt; columns; column++) {
        <span class="hljs-keyword">let</span> currentY = south + deltaY;
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> row = <span class="hljs-number">0</span>; row &lt; rows; row++) {
            <span class="hljs-keyword">const</span> cellPoly = polygon([[
                [currentX, currentY],
                [currentX, currentY + cellHeightDeg],
                [currentX + cellWidthDeg, currentY + cellHeightDeg],
                [currentX + cellWidthDeg, currentY],
                [currentX, currentY],
            ]], options.properties);
            <span class="hljs-keyword">if</span> (options.mask) {
                <span class="hljs-keyword">if</span> (intersect(options.mask, cellPoly)) { results.push(cellPoly); }
            } <span class="hljs-keyword">else</span> {
                results.push(cellPoly);
            }

            currentY += cellHeightDeg;
        }
        currentX += cellWidthDeg;
    }
    <span class="hljs-keyword">return</span> featureCollection(results);
</code></pre>
<h1 id="hexgrid">hexGrid</h1>
<p>&#x63CF;&#x8FF0;&#xFF1A;</p>
<p>&#x200B;    &#x521B;&#x5EFA;&#x6B63;&#x516D;&#x8FB9;&#x5F62;&#x7F51;&#x683C;</p>
<p>&#x5B9E;&#x73B0;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="hljs-comment">/**
 * &#x521B;&#x5EFA;&#x516D;&#x8FB9;&#x5F62;
 *
 * @private
 * @param {Array&lt;number&gt;} center &#x516D;&#x8FB9;&#x5F62;&#x4E2D;&#x5FC3;
 * @param {number} rx &#x516D;&#x8FB9;&#x5F62;&#x5BBD;&#x5EA6;&#x4E00;&#x534A;
 * @param {number} ry &#x516D;&#x8FB9;&#x5F62;&#x9AD8;&#x5EA6;&#x4E00;&#x534A;
 * @param {Object} properties &#x5C5E;&#x6027;&#x503C;
 * @param {Array&lt;number&gt;} cosines &#x6B63;&#x5F26;&#x6570;&#x7EC4;
 * @param {Array&lt;number&gt;} sines &#x4F59;&#x5F26;&#x6570;&#x7EC4;
 * @returns {Feature&lt;Polygon&gt;} hexagon
 */</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">hexagon</span>(<span class="hljs-params">center, rx, ry, properties, cosines, sines</span>) </span>{
    <span class="hljs-keyword">const</span> vertices = [];
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">6</span>; i++) {
        <span class="hljs-keyword">const</span> x = center[<span class="hljs-number">0</span>] + rx * cosines[i];
        <span class="hljs-keyword">const</span> y = center[<span class="hljs-number">1</span>] + ry * sines[i];
        vertices.push([x, y]);
    }
    <span class="hljs-comment">//&#x95ED;&#x5408;</span>
    vertices.push(vertices[<span class="hljs-number">0</span>].slice());
    <span class="hljs-keyword">return</span> polygon([vertices], properties);
}

<span class="hljs-keyword">const</span> cosines = [];
<span class="hljs-keyword">const</span> sines = [];
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">6</span>; i++) {
    <span class="hljs-keyword">const</span> angle = <span class="hljs-number">2</span> * <span class="hljs-built_in">Math</span>.PI / <span class="hljs-number">6</span> * i;
    cosines.push(<span class="hljs-built_in">Math</span>.cos(angle));
    sines.push(<span class="hljs-built_in">Math</span>.sin(angle));
}
</code></pre>
<h1 id="pointgrid">pointGrid</h1>
<p>&#x63CF;&#x8FF0;&#xFF1A;</p>
<p>&#x200B;    &#x521B;&#x5EFA;marker&#x7F51;&#x683C;&#xFF0C;&#x53C2;&#x8003;[squareGrid]</p>
<h1 id="trianglegrid">triangleGrid</h1>
<p>&#x63CF;&#x8FF0;&#xFF1A;</p>
<p>&#x200B;    &#x521B;&#x5EFA;&#x4E09;&#x89D2;&#x5F62;&#x7F51;&#x683C;&#xFF0C;&#x53C2;&#x8003;[hexGrid]</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="chapter10.html" class="navigation navigation-prev " aria-label="Previous page: 10、JOINS">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="chapter12.html" class="navigation navigation-next " aria-label="Next page: 12、CLASSIFICATION">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"11、GRIDS","level":"1.12","depth":1,"next":{"title":"12、CLASSIFICATION","level":"1.13","depth":1,"path":"Chapter/chapter12.md","ref":"Chapter/chapter12.md","articles":[]},"previous":{"title":"10、JOINS","level":"1.11","depth":1,"path":"Chapter/chapter10.md","ref":"Chapter/chapter10.md","articles":[]},"dir":"ltr"},"config":{"plugins":["sharing","livereload"],"styles":{"website":"./styles/website.css"},"pluginsConfig":{"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["weibo","google","twitter","weibo","instapaper"]},"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"lijian","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"turf源码算法分析","language":"zh-hans","gitbook":"3.2.3","description":"turf源码算法分析"},"file":{"path":"Chapter/chapter11.md","mtime":"2021-03-14T00:56:59.124Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-03-28T04:08:06.675Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

